package day0306;

import org.w3c.dom.Node;

public class MyLink {

    // 头
    private Node head;

    // 尾
    private Node tail;

    private int size;

    public void add(Object element) {
        if (null == element) {
            throw new NullPointerException("现在不能存 null");
        }
        Node curr = new Node(element, null);
        if (tail == null) {
            head = curr;
        } else {
            tail.next = curr;
        }
        tail = curr;
        size++;
    }

    public Object remove(int index) {
        // TODO(下标是否合法)
        Node obj = null;
        if (index == 0) {
            obj = head;
            head = head.next;

            // 如果只有一个元素
            if (head == tail) {
                tail = null;
            }
        } else {
            // 被删元素的前一个元素
            Node pre = head;
            // 被删除的元素
            Node node = head.next;
            for (int i = 1; i < index; i++) {
                pre = node;
                node = node.next;
            }
            obj = node;

            pre.next = node.next;
            // 如果删除的是最后一个元素
            if (index == (size - 1)) {
                tail = pre;
            }
        }

        size--;
        return obj.value;
    }

    public Object remove(Object element) {
        // 被删除的元素
        Node node = null;
        if (head.value.equals(element)) {
            node = head;
            head = head.next; // 删除
            // 如果只有一个元素
            if (head == tail) {
                tail = null;
            }
            size--;
            return node.value;
        } else {
            Node pre = head;
            node = head.next;
            while (node != null) {
                if (node.value.equals(element)) {
                    // 相等
                    pre.next = node.next;
                    // 如果删除的是最后一个元素
                    if (node == tail) {
                        tail = pre;
                    }
                    size--;
                    return node.value;
                } else {
                    pre = node;
                    node = node.next;
                }
            }
        }
        return null;
    }

    public Object get(int index) {
        // TODO(index 合法性)
        if (index == 0) {
            return head.value;
        } else {
            Node node = head.next;
            for (int i = 1; i < index; i++) {
                node = node.next;
            }
            return node.value;
        }

    }

    public int size() {
        return this.size;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("MyLink[");
        Node node = head;

        while (node != null) {
            sb.append(node.value + ", ");
            node = node.next;
        }
        return sb.substring(0, sb.length() - 2) + "]";
    }


    private class Node {
        private Object value;

        private Node next;

        public Node(Object value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
}
